package com.wicket.okrapp.common.util.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.wicket.okrapp.common.dto.TreeNode;

import java.io.IOException;


/**
 * @author ：CHS
 * @since 2022/5/25 10:22
 *
 *
 * @version 1.0
 */
public class TreeNodeJsonSerializer extends JsonSerializer<TreeNode> {

    @Override
    public void serialize(TreeNode node, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (node != null) {
            String selfJson = JSON.toJSONString(node.getSelf());
            JSONObject jo = JSONObject.parseObject(selfJson);
            jo.put("children", node.getChildrenList());
            jsonGenerator.writeObject(jo);
        } else {
            jsonGenerator.writeNull();
        }
    }
}
